package mi.payment.utils.transaction;

import android.text.TextUtils;

import java.util.Map;

import mi.payment.utils.iso8583.MsgField;
import mi.payment.utils.iso8583.Utils;
import mi.payment.utils.secure.TerminalSecure;
import sunmi.sunmiui.utils.LogUtil;

/**
 * 二维码查询的返回报文checker （不校验流水号）
 *
 * @author tomcat on 2017/5/15.
 */

class QRCodeQueryChecker implements ReceiveMsgCheck {

    private static final String TAG = "QRCodeQueryChecker";

    /**
     * @param responseBytes 返回包
     * @return mac失败 B6，其他 12 成功 00
     */
    public String check(byte[] requestBytes, Map<String, MsgField> requestMap, byte[] responseBytes, Map<String, MsgField> responseMap) {
        if (requestMap == null || responseMap == null) {
            return "96";
        }
        if (responseBytes == null) {
            return "12";
        }

        MsgField reqField;
        MsgField respField;

        // 1：判断是否有64域，如果有64域，进行mac判断
        // 暂时注释 便于调试
        MsgField F064 = responseMap.get("F064");
        if (F064 != null) {
            String F64_Value = F064.getDataStr();
            if (TextUtils.isEmpty(F64_Value)) {
                return "B6";
            } else {
                TerminalSecure.GenerateMAC(responseBytes);
                byte[] mac = new byte[8];
                System.arraycopy(responseBytes, responseBytes.length - 8, mac, 0, 8);
                String macStr = Utils.byte2HexStr(mac);
                LogUtil.e(TAG, "local mac:" + macStr + " posp mac:" + F64_Value);
                if (!macStr.equals(F64_Value))
                    return "B6";
            }
        }

        // 2：判断消息类型，是否增加10
        byte[] requestTypeBytes = new byte[2];
        System.arraycopy(requestBytes, 13, requestTypeBytes, 0, 2);
        requestTypeBytes[1] = (byte) (requestTypeBytes[1] + 0x10);
        String responseTypeStr = Utils.byte2HexStr(requestTypeBytes);
        if (!responseMap.get("type").getDataStr().equals(responseTypeStr)) {
            return "12A";
        }

        // 3: 比较交易处理码，第一字节必须完全一致
        reqField = requestMap.get("F003");
        respField = responseMap.get("F003");
        if (isTransTypeError(reqField, respField)) {
            return "12A";
        }

        // 4: 比较流水号，必须完全一致
        // reqField = request.get("F011");
        // respField = response.get("F011");
        // if (reqField != null && isError(reqField, respField)) {
        //    return "12A";
        // }

        // 5: 比较终端号，必须完全一致。
        reqField = requestMap.get("F041");
        respField = responseMap.get("F041");
        if (reqField != null && isError(reqField, respField)) {
            return "12A";
        }

        // 6: 比较商户号，必须完全一致。
        reqField = requestMap.get("F042");
        respField = responseMap.get("F042");
        if (reqField != null && isError(reqField, respField)) {
            return "12A";
        }

        // 7: 比较金额，如果不是部分扣款交易，需要完全一致。
        reqField = requestMap.get("F004");
        respField = responseMap.get("F004");
        if (isAmountError(reqField, respField)) {
            return "64";
        }
        return "00";
    }

    private boolean isEmpty(MsgField field) {
        return (field == null || field.getDataStr() == null);
    }

    private boolean isError(MsgField reqField, MsgField respField) {
        if (isEmpty(respField)) {
            return true;
        }
        return !isEmpty(reqField) && !reqField.getDataStr().equals(respField.getDataStr());
    }

    private boolean isAmountError(MsgField reqField, MsgField respField) {
        return !isEmpty(reqField) && !isEmpty(respField) && !reqField.getDataStr().equals(respField.getDataStr());
    }

    private boolean isTransTypeError(MsgField reqField, MsgField respField) {
        return !isEmpty(reqField) && !isEmpty(respField) && !reqField.getDataStr().substring(0, 2).equals(respField.getDataStr().substring(0, 2));
    }

}
